﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using SvnCore;

namespace SVNTreeViewer.Views
{
    /// <summary>
    /// Interaction logic for RevisionBlock.xaml
    /// </summary>
    public partial class RevisionBlock : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RevisionBlock"/> class.
        /// </summary>
        public RevisionBlock()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RevisionBlock"/> class.
        /// </summary>
        /// <param name="revision">The revision.</param>
        public RevisionBlock(Revision revision)
        {
            InitializeComponent();
            this.InitializeRevision(revision);
        }

        /// <summary>
        /// Gets or sets the index.
        /// </summary>
        /// <value>
        /// The index.
        /// </value>
        public long Index { get; set; }

        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        /// <value>
        /// The location.
        /// </value>
        public Point Location { get; set; }

        /// <summary>
        /// Gets or sets the revision.
        /// </summary>
        /// <value>
        /// The revision.
        /// </value>
        public Revision Revision { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether [wants to connect].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [wants to connect]; otherwise, <c>false</c>.
        /// </value>
        public bool WantsToConnect { get; set; }

        /// <summary>
        /// Gets or sets the revision to connect to.
        /// </summary>
        /// <value>
        /// The revision to connect to.
        /// </value>
        public RevisionBlock RevisionToConnectTo { get; set; }

        /// <summary>
        /// Adds the additional tool tip data.
        /// </summary>
        public void AddAdditionalToolTipData()
        {
            StringBuilder sb = new StringBuilder();
            ////sb.Append(this.ToolTip);
            ////sb.AppendLine("Merge from revisions(" + this.Revision.MergeFromRevisions.Count + "):");

            foreach (Revision mergeFromRevision in this.Revision.MergeFromRevisions)
            {
                sb.AppendLine(mergeFromRevision.Number.ToString());
            }

            this.ToolTip = sb;

            if (this.Revision.MergeFromRevisions.Any())
            {
                this.ControlBackground.Background = new SolidColorBrush(Colors.Orange);
            }
        }

        /// <summary>
        /// Initializes the revision.
        /// </summary>
        /// <param name="revision">The revision.</param>
        private void InitializeRevision(Revision revision)
        {
            this.Revision = revision;
            this.ControlText.Text = revision.Number.ToString();
            this.Index = revision.Index;

            StringBuilder sb = new StringBuilder();
            foreach (string property in revision.Properties)
            {
                sb.AppendLine(property);
            }

            sb.AppendLine("Index:" + revision.Index);
            this.ToolTip = sb;
        }
    }
}
